/**
 * Author:  Sanket Hiremath
   ----------------------------------------------------------------------
   	Copyright (C) Sanket Hiremath, 2020

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

   ----------------------------------------------------------------------
 */

#include "opt3001.h"
#include "stdio.h"
#include "oled.h"
extern char show_buff[20];
// extern I2C_HandleTypeDef hi2c1;


// static u8 device_addr = OPT3001_ADDR1;
static u16 config_data= 0xCE10;        //The sensor works in continuous operation mode by default.


void opt3001_writedata(IIC_enum IIC_channel,u8 dev,u8 register_addr, u16 data){

	u8 buffer[2];
	buffer[0] = (data)>>8;
	buffer[1] = (data)& 0x00FF;
	//  HAL_I2C_Master_Transmit(&hi2c1, device_addr<<1, buffer, 3, 50);
	//printf("config: %#X ,%#X\r\n",buffer[0],buffer[1]);
	IICwriteBytes(IIC_channel,dev, register_addr, 2,buffer);
 }

u16 opt3001_readdata(IIC_enum IIC_channel,u8 dev,u8 reg){

	//u8 readbuffer[2];
	u8 rx_data[2]={0};
	u16 received_data;
	//HAL_I2C_Master_Receive(&hi2c1, device_addr<<1, readbuffer, 2, 50);
	IICreadBytes(IIC_channel,dev<<1, reg, 2,rx_data);
	//received_data = ((readbuffer[0] << 8) | readbuffer[1]);
	received_data=rx_data[0]*256+rx_data[1];
	return received_data;
}

u8 opt3001_init(IIC_enum IIC_channel,u8 dev){

	// if (HAL_I2C_IsDeviceReady(&hi2c1, device_addr<<1, 1, 20000) != HAL_OK){
	// 	/* Return false */
	// 	return 0;
	// }
	IIC_Init(IIC_channel);
	//delay_ms(20);
	opt3001_writedata(IIC_channel,dev<<1, OPT3001_ConfigReg, config_data);
	return 1;
}


void calculate_lux(IIC_enum IIC_channel,u8 dev,float* light ,u16* rwa_hex){
	
	u16 iExponent, iMantissa;
	//float final_lux;
	u16 rawlux;

	//opt3001_writedata(device_addr, OPT3001_ResultReg, 0x00);
	//delay_ms(50);
	//delay_ms(50);
	rawlux = opt3001_readdata(IIC_channel,dev,OPT3001_ResultReg);
	*rwa_hex=rawlux;//back up ,in case it changed
	//sprintf(show_buff,"R:0x%4X",rawlux);
	//OLED_ShowString(0,0,show_buff,16);
	//printf("raw: %#X\r\n",rawlux);
	iMantissa = rawlux & 0x0FFF;
	iExponent = (rawlux & 0xF000) >> 12;
	//final_lux = iMantissa * (0.01 * powf(2, iExponent));
	*light = iMantissa * ( powf(2, iExponent));
	//传感器未连接( FFFF )的计算结果
	if(*light==134184960)*light=-1;
//	return final_lux;
}

u16 read_devid(IIC_enum IIC_channel,u8 dev){

	u16 received_data;
   // opt3001_writedata(device_addr, OPT3001_DeviceID, 0x00);
    //delay_ms(50);
    received_data = opt3001_readdata(IIC_channel,dev,OPT3001_DeviceID);

	return received_data;
}

u16 read_manufacturer_id(IIC_enum IIC_channel,u8 dev){

	u16 received_data;
    //opt3001_writedata(device_addr, OPT3001_ManuID, 0x00);
    //delay_ms(50);
    received_data = opt3001_readdata(IIC_channel,dev,OPT3001_ManuID);

	return received_data;
}

/*
功能：读取传感器数据
输入：无
输出：无
*/
/*
#define ARRY_SIZE 10
u32 opt3001_data[ARRY_SIZE];
u8 arry_wpr=0;
 
u8 opt3001_get_lux(void)
{
	u8		vRval 	= 0;
	u16  	vCfg 	= 0;
	u16  	vDat 	= 0;
	
	u16  	vDatE = 0;
	u16  	vDatR = 0;
	
	float   vFval 		= 0.0;
	float   vLsbSize 	= 0.0;
	float   vFlux 		= 0;
	
	vCfg = opt3001_readdata(OPT3001_ConfigReg);
	vCfg |= (0x01<<9);
	opt3001_writedata(device_addr,OPT3001_ConfigReg, vCfg);		//单次采集光照
	
	vCfg = opt3001_readdata(OPT3001_ConfigReg);
	delay_ms(900);								//大于800Ms
	
	vCfg = opt3001_readdata(OPT3001_ConfigReg);
	if((vCfg&(0x01<<7)) )						//光照采集完成
	{
		vDat = opt3001_readdata(OPT3001_ResultReg);
		
		vDatE = ((vDat&0xF000)>>12);
		vDatR = (vDat&0x0FFF);
		
		vFval = (0x01<<vDatE);
		vLsbSize = (0.01f * vFval);
		
		vFlux  = (vLsbSize * (float)vDatR);
		opt3001_data[arry_wpr] = ((vFlux)*100.0f);//透明外壳不需要矫正 ，乳白色外壳推荐*1.8矫正 *vp_Lux = ((vFlux*1.8)*100.0)
		arry_wpr++;	
		if(arry_wpr >= ARRY_SIZE)		
		{
		arry_wpr = 0;
		}
	}
	else
	{
		vRval = 0x01;//光照采集失败
	}
	
	return vRval;
}
*/
